<?php
namespace app\mall\controller;
use think\Config;
use think\Db;

class User extends Base
{	
	/* 获取用户数据 */
	public function index(){
		$uid = $this->uid;
		$user = Db::field('u.username,u.realname,u.nickname,u.avatar,u.phone,u.user_money,s.shop_id,s.shop_name')
		->table('sdzx_user')->alias('u')
		->join('sdzx_shop s','u.shop_id=s.shop_id','left')
		->where("id=$uid")
		->find();
		$this->success('success','',$user);
	}

	/**
	 * 用户地址列表
	 * @return [type] [description]
	 */
	public function address(){
		$uid = $this->uid;
		$shop = Db::name('shop')->where(['user_id'=>$uid])->find();
		$list = Db::name('user_address')->where(['user_id'=>$uid])->order('type desc')->select();
		foreach ($list as $k => $v) {
			if (empty($list[$k]['shop_name'])) {
				$list[$k]['shop_name'] = $shop['shop_name'];
			}
		}
		$this->success('success','',$list);
	}

	/**
	 * 获取指定ID地址
	 * @return [type] [description]
	 */
	public function getAddress()
	{
		$uid = $this->uid;
		$params = Request()->param();
		$info=[];
		if (isset($params['address_id']) && !empty($params['address_id'])) {
			$info = Db::field('a.*,s.shop_name as shopname')
			->table('sdzx_user_address')->alias('a')
			->join('sdzx_shop s','a.user_id=s.user_id','left')
			->where(['a.address_id'=>$params['address_id'],'a.user_id'=>$uid])
			->order('a.type desc')->find();
			if (empty($info['shop_name'])) {
				$info['shop_name'] = $info['shopname'];
			}
		}
		$this->success('success','',$info);
	}

	/**
	 * 添加地址
	 */
	public function addAddress(){
		$uid = $this->uid;
		// $params = Request()->only('province,city,district,address,name,phone,tel,type,address_id');
		$params = Request()->param();
		

		$data['user_id'] = $uid;
		if(isset($params['type'])){
			$data['type'] = $params['type']?1:0;
		}
		
		if(isset($params['address'])){
			$data['pcd'] = $params['address'];
		}

		if(isset($params['details'])){
			$data['address'] = $params['details'];
		}

		if(isset($params['phone'])){
			$data['phone'] = $params['phone'];
		}

		if(isset($params['name'])){
			$data['name'] = $params['name'];
		}

		if(isset($params['shop_name'])){
			$data['shop_name'] = $params['shop_name'];
			$shop = Db::name('shop')->where(['user_id'=>$uid])->find();
			if (empty($shop['shop_name'])) {
				Db::name('shop')->where(['user_id'=>$uid])->update(['shop_name'=>$data['shop_name']]);
			}
		}
		
		$Query = Db::name('user_address');

		if (isset($params['address_id']) && !empty($params['address_id'])) {
			$re = $Query->where(['address_id'=>$params['address_id']])->update($data);
			$address_id = $params['address_id'];
		}else{
			$re = $Query->insertGetId($data);
			$address_id = $re;
		}

		

		if ($re!==false) {
			if ($data['type']==1) {
				$Query->where(['address_id'=>['neq',$address_id],'user_id'=>$this->uid])->update(['type'=>0]);
			}
			$this->success('success','','');
		}else{
			$this->error('error');
		}
	}

	/**
	 * 删除地址
	 * @return [type] [description]
	 */
	public function delAddress(){
		$data = Request()->param();
    	!$data['ids'] && $this->error('参数错误');
    	$re = Db::name('user_address')->where(['user_id'=>$this->uid,'address_id'=>['in',$data['ids']]])->delete();
    	if ($re!==false) {
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
	}
	
	/* 提交需求 */
	public function demand(){
		$data = [
			'good_name' => request()->param('good_name')?:'',
			'good_cate' => request()->param('good_cate')?:'',
			'good_brand' => request()->param('good_brand')?:'',
			'good_spec' => request()->param('good_spec')?:'',
			'agent' => request()->param('agent')?:'',
			'price' => request()->param('price'),
			'remark' => request()->param('remark')?:'',
			'contact' => request()->param('contact'),
			'submit_time' => date('Y-m-d H:i:s',time()),
			'status' => 0,
			'user_id' => $this->user['id']
		];
		$res = db('demand')->insert($data);
		if($res!==false){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
	
	/* 店铺信息 */
	function shop_details_commit(){
		$user_id = $this->user['id'];
		$param = request()->param();
		if($param['contact_phone']){
			$data = [
				'shop_name' => input('shop_name'),
				'shop_owner' => input('shop_people'),
				'shop_address' => input('shop_address'),
				'user_id' => $user_id,
				'create_time' => date('Y-m-d H:i:s',time()),
				'contact_phone' => input('contact_phone'),
				'sc_id' => input('cate_id')
			];
			$shop = db('shop')->where('user_id',$user_id)->find();
			if($shop){
				$res = db('shop')->where('shop_id',$shop['shop_id'])->update($data);
				if($res !== false){
					$this->success('操作成功','',$shop);
				}else{
					$this->error('操作失败');
				}
			}else{
				$user = db('user')->where('id',$user_id)->find();
				$data['shop_no'] = $user['username'];
				$data['user_id'] = $user_id;
				$res = db('shop')->insertGetId($data);
				if($res){
					db('user')->where('id',$user_id)->update(['shop_id' => $res]);
					$this->success('操作成功');
				}else{
					$this->error('操作失败');
				}
			}
		}else{
			$shop = db('shop')->where('user_id',$user_id)->find();
			$shop_cat = Db::name('shop_category')->order('sc_sort asc')->select();
			$shop['cate'] = $shop_cat;
			$this->success('','',$shop);
		}
	}
	
	/* 获取分类 */
	public function cate(){
		$cate = db('goods_category')->where('cate_pid',0)->select();
		if($cate){
			$this->success('success','',$cate);
		}
	}
	
	/* 发票信息 */
	function invoice(){
		$user_id = $this->user['id'];
		$param = request()->param();
		if($param['license_name']){
			$data = [
				'license_type' => input('license_type'),
				'license_name' => input('license_name'),
				'id_number' => input('id_number'),
				'address' => input('address'),
				'user_id' => $user_id,
				'start_time' => request()->param('start_time'),
				'time' => input('time'),
				'phone' => input('phone'),
				'bank' => input('bank'),
				'bank_account' => input('bank_account'),
				'email' => input('email')
			];
			$invoice = db('invoice')->where('user_id',$user_id)->find();
			if($invoice){
				$res = db('invoice')->where('id',$invoice['id'])->update($data);
			}else{
				$res = db('invoice')->insert($data);
			}
			if($res !== false){
				$this->success('操作成功');
			}else{
				$this->error('操作失败');
			}
		}else{
			$invoice = db('invoice')->where('user_id',$user_id)->find();
			$config['sites'] = Config::get('sites');
			$invoice['invoice'] = explode("\n",$config['sites']['invoice']);
			if($invoice){
				$invoice['start_time'] = date('Y-m-d',strtotime($invoice['start_time']));
				$invoice['time'] = date('Y-m-d',strtotime($invoice['time']));
			}
			$this->success('','',$invoice);
		}
	}
}